/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.psc.controllers;

import com.mycompany.psc.data.TenderType;
import com.mycompany.psc.ejb.ReportableItemsManager;
import com.mycompany.psc.entities.Aluminium;
import com.mycompany.psc.entities.Carpenter;
import com.mycompany.psc.entities.MetalDoors;
import com.mycompany.psc.entities.Mobilization;
import com.mycompany.psc.entities.Projects;
import com.mycompany.psc.entities.SiteExpenses;
import com.mycompany.psc.entities.TenderItems;
import com.mycompany.psc.helpers.ReportData;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.event.NodeUnselectEvent;
import org.primefaces.model.CheckboxTreeNode;
import org.primefaces.model.TreeNode;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.PieChartModel;

/**
 *
 * @author A7med
 */
@Named
@SessionScoped
public class ReportsController implements Serializable {

    private Projects p1;
    private Projects p2;
    private TenderType[] selectedTyeps;
    private TreeNode[] selectedNodes;
    private PieChartModel model1=new PieChartModel();
    private PieChartModel model2=new PieChartModel();
    @EJB
    ReportableItemsManager rm;
    private double total1;
    private double total2;
    private CartesianChartModel barModel = new CartesianChartModel();
    private TreeNode codesTree=null;

    public PieChartModel getModel1() {
        model1.clear();
         if (selectedNodes != null) {
            for (TreeNode t : selectedNodes) {
                Object current = t.getData();
                if (current instanceof TenderItems) {
                    TenderItems t1 = (TenderItems) current;
                    TenderItems t2 = (TenderItems) rm.getResource("TenderItems.findByCodeAndProject", p2, t1.getCode());
                    model1.set(t1.getCode(), t1.getTotalValue());
                } else if (current instanceof Aluminium) {
                    Aluminium a1 = (Aluminium) current;
                    Aluminium a2 = (Aluminium) rm.getResource("Aluminium.findByCodeAndProject", p2, a1.getCode());
                    model1.set(a1.getCode(), a1.getTotalValue());
                } else if (current instanceof Carpenter) {
                    Carpenter a1 = (Carpenter) current;
                    Carpenter a2 = (Carpenter) rm.getResource("Carpenter.findByCodeAndProject", p2, a1.getCode());
                     model1.set(a1.getCode(), a1.getTotalValue());
                } else if (current instanceof Mobilization) {
                    Mobilization a1 = (Mobilization) current;
                    Mobilization a2 = (Mobilization) rm.getResource("Mobilization.findByCodeAndProject", p2, a1.getCode());
                    model1.set(a1.getCode(), a1.getTotalValue());
                } else if (current instanceof MetalDoors) {
                    MetalDoors a1 = (MetalDoors) current;
                    MetalDoors a2 = (MetalDoors) rm.getResource("MetalDoors.findByCodeAndProject", p2, a1.getCode());
                    model1.set(a1.getCode(), a1.getTotalValue());
                }
            }
        }
        
        return model1;
    }

    public void setModel1(PieChartModel model1) {
        this.model1 = model1;
    }

    public PieChartModel getModel2() {
        model2.clear();
        if (selectedNodes != null) {
            for (TreeNode t : selectedNodes) {
                Object current = t.getData();
                if (current instanceof TenderItems) {
                    TenderItems t1 = (TenderItems) current;
                    TenderItems t2 = (TenderItems) rm.getResource("TenderItems.findByCodeAndProject", p2, t1.getCode());
                    model2.set(t2.getCode(), t2.getTotalValue());
                } else if (current instanceof Aluminium) {
                    Aluminium a1 = (Aluminium) current;
                    Aluminium a2 = (Aluminium) rm.getResource("Aluminium.findByCodeAndProject", p2, a1.getCode());
                    model2.set(a2.getCode(), a2.getTotalValue());
                } else if (current instanceof Carpenter) {
                    Carpenter a1 = (Carpenter) current;
                    Carpenter a2 = (Carpenter) rm.getResource("Carpenter.findByCodeAndProject", p2, a1.getCode());
                     model2.set(a2.getCode(), a2.getTotalValue());
                } else if (current instanceof Mobilization) {
                    Mobilization a1 = (Mobilization) current;
                    Mobilization a2 = (Mobilization) rm.getResource("Mobilization.findByCodeAndProject", p2, a1.getCode());
                    model2.set(a2.getCode(), a2.getTotalValue());
                } else if (current instanceof MetalDoors) {
                    MetalDoors a1 = (MetalDoors) current;
                    MetalDoors a2 = (MetalDoors) rm.getResource("MetalDoors.findByCodeAndProject", p2, a1.getCode());
                    model2.set(a2.getCode(), a2.getTotalValue());
                }
            }
        }
        return model2;
    }

    public void setModel2(PieChartModel model2) {
        this.model2 = model2;
    }


    public void onNodeSelect(NodeSelectEvent event) {
        TreeNode n = event.getTreeNode();
        //System.out.println("p1:"+n.getData().toString()+" and p2:"+n.getType());
        n.setSelected(true);
    }

    public void onNodeUnSelect(NodeUnselectEvent event) {
        TreeNode n = event.getTreeNode();
        n.setSelected(false);
    }

    public CartesianChartModel getBarModel() {
        barModel.clear();
        ChartSeries pr1 = new ChartSeries();
        pr1.setLabel(p1.getName());
        ChartSeries pr2 = new ChartSeries();
        pr2.setLabel(p2.getName());
        if (selectedNodes != null) {
            for (TreeNode t : selectedNodes) {
                Object current = t.getData();
                if (current instanceof TenderItems) {
                    TenderItems t1 = (TenderItems) current;
                    TenderItems t2 = (TenderItems) rm.getResource("TenderItems.findByCodeAndProject", p2, t1.getCode());
                    pr1.set(t1.getCode(), t1.getTotalValue());
                    pr2.set(t2.getCode(), t2.getTotalValue());
                } else if (current instanceof Aluminium) {
                    Aluminium a1 = (Aluminium) current;
                    Aluminium a2 = (Aluminium) rm.getResource("Aluminium.findByCodeAndProject", p2, a1.getCode());
                    pr1.set(a1.getCode(), a1.getTotalValue());
                    pr2.set(a2.getCode(), a2.getTotalValue());
                } else if (current instanceof Carpenter) {
                    Carpenter a1 = (Carpenter) current;
                    Carpenter a2 = (Carpenter) rm.getResource("Carpenter.findByCodeAndProject", p2, a1.getCode());
                    pr1.set(a1.getCode(), a1.getTotalValue());
                    pr2.set(a2.getCode(), a2.getTotalValue());
                } else if (current instanceof Mobilization) {
                    Mobilization a1 = (Mobilization) current;
                    Mobilization a2 = (Mobilization) rm.getResource("Mobilization.findByCodeAndProject", p2, a1.getCode());
                    pr1.set(a1.getCode(), a1.getTotalValue());
                    pr2.set(a2.getCode(), a2.getTotalValue());
                } else if (current instanceof MetalDoors) {
                    MetalDoors a1 = (MetalDoors) current;
                    MetalDoors a2 = (MetalDoors) rm.getResource("MetalDoors.findByCodeAndProject", p2, a1.getCode());
                    pr1.set(a1.getCode(), a1.getTotalValue());
                    pr2.set(a2.getCode(), a2.getTotalValue());
                }
            }
        }
        barModel.addSeries(pr1);
        barModel.addSeries(pr2);
        return barModel;
    }

    public void setBarModel(CartesianChartModel barModel) {
        this.barModel = barModel;
    }

    public TreeNode[] getSelectedNodes() {
        return selectedNodes;
    }

    public void setSelectedNodes(TreeNode[] selectedNodes) {
        this.selectedNodes = selectedNodes;
    }

    public TreeNode codesTree() {
        if(codesTree!=null){
            return codesTree;
        }else{
        TreeNode root = new CheckboxTreeNode("Codes", null);
        for (TenderType parent : TenderType.values()) {

            addChildren(parent, root);
        }
        codesTree=root;
        return root;
        }
    }

    private void addChildren(TenderType t, TreeNode root1) {
        if (t.isGeneral()) {
            List<TenderItems> p1List = rm.getResources(t, p1);
            for (TenderItems t1 : p1List) {
                TreeNode node0 = new CheckboxTreeNode(t1, root1);
            }
        } else {

            switch (t) {
                case ALUMINUM:
                    List<Aluminium> p1AluList = rm.getAluminiumResources(p1);

                    for (Aluminium a : p1AluList) {
                        TreeNode node0 = new CheckboxTreeNode(a, root1);
                    }
                    break;
                case CARPENTER:
                    List<Carpenter> p1CarpList = rm.getCarpenterResources(p1);

                    for (Carpenter a : p1CarpList) {
                        TreeNode node0 = new CheckboxTreeNode(a, root1);
                    }
                    break;
                case MOBILIZATION:
                    List<Mobilization> p1MobpList = rm.getMobilizationResources(p1);

                    for (Mobilization a : p1MobpList) {
                        TreeNode node0 = new CheckboxTreeNode(a, root1);
                    }
                    break;
                case STEEL_DOORS:
                    List<MetalDoors> p1SteelpList = rm.getSteelDoorsResources(p1);

                    for (MetalDoors a : p1SteelpList) {
                        TreeNode node0 = new CheckboxTreeNode(a, root1);
                    }
                    break;
                case SITE_EXPENCES:
                    List<SiteExpenses> p1ExpList = rm.getSiteExpensesResources(p1);
                    for (SiteExpenses a : p1ExpList) {
                        TreeNode node0 = new CheckboxTreeNode(a, root1);
                    }
                    break;
            }

        }

    }

    public double getTotal1() {
        return total1;
    }

    public double getTotal2() {
        return total2;
    }

    /**
     * @return the p1
     */
    public Projects getP1() {
        return p1;
    }

    /**
     * @param p1 the p1 to set
     */
    public void setP1(Projects p1) {
        this.p1 = p1;
    }

    /**
     * @return the p2
     */
    public Projects getP2() {
        return p2;
    }

    /**
     * @param p2 the p2 to set
     */
    public void setP2(Projects p2) {
        this.p2 = p2;
    }

    /**
     * @return the selectedTyeps
     */
    public TenderType[] getSelectedTyeps() {
        return selectedTyeps;
    }

    /**
     * @param selectedTyeps the selectedTyeps to set
     */
    public void setSelectedTyeps(TenderType[] selectedTyeps) {
        this.selectedTyeps = selectedTyeps;
    }

    public ArrayList<TenderType> getDeparts() {
        ArrayList<TenderType> tabs = new ArrayList<>();
        tabs.addAll(Arrays.asList(TenderType.values()));
        return tabs;
    }

    public List<ReportData> getReportData() {
        total1 = 0;
        total2 = 0;
      

        List<ReportData> reportData = new ArrayList<>();
        if (selectedNodes != null) {
              barModel = new CartesianChartModel();
        ChartSeries pr1 = new ChartSeries();
        pr1.setLabel(p1.getName());

        ChartSeries pr2 = new ChartSeries();
        pr2.setLabel(p2.getName());
            for (TreeNode t : selectedNodes) {
                Object current = t.getData();
                if (current instanceof TenderItems) {
                    TenderItems t1 = (TenderItems) current;

                    double p1Value = 0;

                    p1Value += t1.getTotalValue();

                    TenderItems t2 = (TenderItems) rm.getResource("TenderItems.findByCodeAndProject", p2, t1.getCode());
                    pr1.set(t1.getCode(), t1.getTotalValue());
                    pr2.set(t2.getCode(), t2.getTotalValue());
                    total1 += p1Value;
                    total2 += t2.getTotalValue();
                    ReportData r = new ReportData(p1Value, t2.getTotalValue(), t1.getCode());
                    reportData.add(r);
                } else if (current instanceof Aluminium) {
                    Aluminium a1 = (Aluminium) current;
                    Aluminium a2 = (Aluminium) rm.getResource("Aluminium.findByCodeAndProject", p2, a1.getCode());
                    total1 += a1.getTotalValue();;
                    total2 += a2.getTotalValue();
                    pr1.set(a1.getCode(), a1.getTotalValue());
                    pr2.set(a2.getCode(), a2.getTotalValue());
                    ReportData r = new ReportData(a1.getTotalValue(), a2.getTotalValue(), a1.getCode());
                    reportData.add(r);
                } else if (current instanceof Carpenter) {
                    Carpenter a1 = (Carpenter) current;
                    Carpenter a2 = (Carpenter) rm.getResource("Carpenter.findByCodeAndProject", p2, a1.getCode());
                    total1 += a1.getTotalValue();
                    total2 += a2.getTotalValue();
                    pr1.set(a1.getCode(), a1.getTotalValue());
                    pr2.set(a2.getCode(), a2.getTotalValue());
                    ReportData r = new ReportData(a1.getTotalValue(), a2.getTotalValue(), a1.getCode());
                    reportData.add(r);
                } else if (current instanceof Mobilization) {
                    Mobilization a1 = (Mobilization) current;
                    Mobilization a2 = (Mobilization) rm.getResource("Mobilization.findByCodeAndProject", p2, a1.getCode());
                    total1 += a1.getTotalValue();
                    total2 += a2.getTotalValue();
                    pr1.set(a1.getCode(), a1.getTotalValue());
                    pr2.set(a2.getCode(), a2.getTotalValue());
                    ReportData r = new ReportData(a1.getTotalValue(), a2.getTotalValue(), a1.getCode());
                    reportData.add(r);
                } else if (current instanceof MetalDoors) {
                    MetalDoors a1 = (MetalDoors) current;
                    MetalDoors a2 = (MetalDoors) rm.getResource("MetalDoors.findByCodeAndProject", p2, a1.getCode());
                    total1 += a1.getTotalValue();
                    total2 += a2.getTotalValue();
                    pr1.set(a1.getCode(), a1.getTotalValue());
                    pr2.set(a2.getCode(), a2.getTotalValue());
                    ReportData r = new ReportData(a1.getTotalValue(), a2.getTotalValue(), a1.getCode());
                    reportData.add(r);
                }

            }
        }
        return reportData;

    }
}
